package com.xjrsoft.module.app.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.page.PageInput;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.app.dto.AppFnSchemeDto;
import com.xjrsoft.module.app.dto.AppFnSchemeListDto;
import com.xjrsoft.module.app.entity.XjrAppFnScheme;
import com.xjrsoft.module.app.service.IXjrAppFnSchemeService;
import com.xjrsoft.module.app.vo.AppFnSchemeListVo;
import com.xjrsoft.module.app.vo.AppFnSchemeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 自定义表单绑定APP设置信息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-02-24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/app-function-scheme")
@Api(value = "/app-function-scheme",tags = "移动功能设计模板模块")
public class XjrAppFnSchemeController {

    private final IXjrAppFnSchemeService fnSchemeService;

    @GetMapping
    @ApiOperation(value = "获取移动功能设计模板列表数据")
    public Response<PageOutput<AppFnSchemeListVo>> page(AppFnSchemeListDto pageInput) {
        return Response.ok(fnSchemeService.getPageList(pageInput));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取移动功能设计模板")
    public Response<AppFnSchemeVo> getById(@PathVariable String id) {
        XjrAppFnScheme appFnScheme = fnSchemeService.getById(id);
        return Response.ok(BeanUtil.copy(appFnScheme, AppFnSchemeVo.class));
    }

    @PostMapping
    @ApiOperation(value = "新增移动功能设计模板")
    public Response addFnScheme(@RequestBody AppFnSchemeDto appFnSchemeDto) {
        XjrAppFnScheme appFnScheme = BeanUtil.copy(appFnSchemeDto, XjrAppFnScheme.class);
        return Response.status(fnSchemeService.saveOrUpdate(appFnScheme));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "新增移动功能设计模板")
    public Response updateFnScheme(@PathVariable String id, @RequestBody AppFnSchemeDto appFnSchemeDto) {
        XjrAppFnScheme appFnScheme = BeanUtil.copy(appFnSchemeDto, XjrAppFnScheme.class);
        appFnScheme.setId(id);
        return Response.status(fnSchemeService.updateById(appFnScheme));
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除移动功能设计模板")
    public Response deleteFnScheme(@PathVariable String ids) {
        String[] idArray = StringUtils.split(ids, StringPool.COMMA);
        boolean isSuccess = false;
        if (idArray.length == 1) {
            isSuccess = fnSchemeService.removeById(ids);
        } else {
            isSuccess = fnSchemeService.removeByIds(Arrays.asList(idArray));
        }
        return Response.ok(isSuccess);
    }
}
